package com.lefevre.cms.service.help.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lefevre.cms.bean.help.Help;
import com.lefevre.cms.mapper.HelpMapper;
import com.lefevre.cms.service.help.HelpService;
import com.lefevre.cms.service.help.HelpTypeService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 帮助管理
 *
 * @author Lefevre
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class HelpServiceImpl extends ServiceImpl<HelpMapper, Help> implements HelpService {

    @Resource
    HelpTypeService helpTypeService;

    /**
     * 根据Id查询帮助
     *
     * @param helpId 帮助Id
     * @return
     */
    @Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED)
    public Help findById(Long helpId) {
        return baseMapper.selectById(helpId);
    }

    /**
     * 根据Id集合查询帮助
     *
     * @param helpIdList 帮助Id集合
     * @return
     */
    @Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED)
    public List<Help> findByIdList(List<Long> helpIdList) {
        return baseMapper.selectBatchIds(helpIdList);
    }

    /**
     * 根据分类Id查询帮助
     *
     * @param helpTypeId 帮助分类Id
     * @return
     */
    @Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED)
    public List<Help> findByTypeId(Long helpTypeId) {
        return baseMapper.selectList(new LambdaQueryWrapper<Help>().eq(Help::getHelpTypeId, helpTypeId)
                .eq(Help::isVisible, true));
    }


    /**
     * 保存帮助
     *
     * @param help
     */
    public Integer saveHelp(Help help) {
        int i = helpTypeService.addHelpQuantity(help.getHelpTypeId());
        baseMapper.insert(help);
        return i;
    }

    /**
     * 修改帮助
     *
     * @param help
     * @return
     */
    public Integer updateHelp(Help help) {
        return baseMapper.updateById(help);
    }

    /**
     * 修改帮助
     *
     * @param helpList       帮助集合
     * @param new_helpTypeId 新帮助分类Id
     * @return
     */
    public Integer updateHelp(List<Help> helpList, Long new_helpTypeId) {
        int i = 0;
        if (helpList != null && helpList.size() > 0) {
            for (Help help : helpList) {
                //旧帮助分类Id
                Long old_helpTypeId = help.getHelpTypeId();
                help.setHelpTypeId(new_helpTypeId);
                i += baseMapper.updateById(help);

                //减少旧帮助数量
                helpTypeService.minusHelpQuantity(old_helpTypeId, 1L);
                //增加新帮助数量
                helpTypeService.addHelpQuantity(new_helpTypeId);
            }
        }
        return i;
    }


    /**
     * 还原帮助
     *
     * @param helpList 帮助集合
     * @return
     */
    public Integer reductionHelp(List<Help> helpList) {
        int i = 0;
        if (helpList != null && helpList.size() > 0) {
            for (Help help : helpList) {
                help.setVisible(true);
                int j = baseMapper.updateById(help);
                i += j;
                if (j > 0) {
                    helpTypeService.mark_addHelpQuantity(help.getHelpTypeId());
                }
            }
        }
        return i;
    }

    /**
     * 标记删除帮助
     *
     * @param helpTypeId 帮助分类Id
     * @param helpId     帮助Id
     * @return
     */
    public Integer markDelete(Long helpTypeId, Long helpId) {
        int i = baseMapper.update(null, new LambdaUpdateWrapper<Help>()
                .set(Help::isVisible, false).eq(Help::getId, helpId));
        if (i > 0) {
            helpTypeService.mark_minusHelpQuantity(helpTypeId);
        }
        return i;
    }

    /**
     * 删除帮助
     *
     * @param helpTypeId   帮助分类Id
     * @param helpId       帮助Id
     * @param helpQuantity 减少帮助数量(显示)
     * @return
     */
    public Integer deleteHelp(Long helpTypeId, Long helpId, Long helpQuantity) {
        int i = baseMapper.deleteById(helpId);
        if (i > 0) {
            helpTypeService.minusHelpQuantity(helpTypeId, helpQuantity);
        }
        return i;
    }

    /**
     * 修改帮助分类Id
     *
     * @param old_HelpTypeId 旧帮助分类Id
     * @param new_HelpTypeId 新帮助分类Id
     * @return
     */
    public Integer updateHelpTypeId(Long old_HelpTypeId, Long new_HelpTypeId) {
        return baseMapper.update(null, new LambdaUpdateWrapper<Help>()
                .set(Help::getHelpTypeId, new_HelpTypeId)
                .eq(Help::getHelpTypeId, old_HelpTypeId));
    }
}
